package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.SeckillActivityDto;
import com.qf.commerce.common.vo.SeckillActivityVo;
import com.qf.commerce.entity.SeckillActivity;
import com.qf.commerce.entity.SeckillActivityProduct;
import com.qf.commerce.entity.SeckillOrder;
import com.qf.commerce.exception.ServiceException;
import com.qf.commerce.mapper.SeckillActivityMapper;
import com.qf.commerce.mapper.SeckillActivityProductMapper;
import com.qf.commerce.mapper.SeckillOrderMapper;
import com.qf.commerce.resp.ResponseStatus;
import com.qf.commerce.service.SeckillActivityService;
import com.qf.commerce.utils.CommonBeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
@Service
public class SeckillActivityServiceImpl implements SeckillActivityService {
    @Resource
    SeckillActivityMapper seckillActivityMapper;
    @Resource
    SeckillOrderMapper seckillOrderMapper;
    @Resource
    SeckillActivityProductMapper seckillActivityProductMapper;
    @Override
    public PageInfo<SeckillActivityVo> querySeckillActivity(Integer page,Integer size,SeckillActivityDto seckillActivityDto) {
        SeckillActivity seckillActivity = CommonBeanUtils.copyForBean(seckillActivityDto, SeckillActivity::new);
        //获取活动场次
        List<SeckillActivity> seckillActivities = seckillActivityMapper.selectSeckillActivity(seckillActivity);
        if(ObjectUtils.isEmpty(seckillActivities)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        //获取所有订单
        int sum = 0;
        int peoples=0;
        List<SeckillOrder> seckillOrders = seckillOrderMapper.selectBySeckillActivityId(seckillActivities);
        for (SeckillActivity activity : seckillActivities) {
            for (SeckillOrder seckillOrder : seckillOrders) {
                if(activity.getSeckillActivityId().equals(seckillOrder.getSkillActivityId())){
                    sum+= seckillOrder.getTotalAmount();
                    peoples++;
                }
            }
            activity.setPeople(peoples);
            activity.setTotalMoney(sum);
            sum=0;
            peoples=0;
        }
        List<SeckillActivityVo> seckillActivityVos = CommonBeanUtils.copyForList(seckillActivities, SeckillActivityVo::new);
        if(ObjectUtils.isEmpty(seckillActivityVos)){
            throw new ServiceException (ResponseStatus.DATA_ERROR);
        }
        PageInfo<SeckillActivity> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> seckillActivityMapper.selectSeckillActivity(seckillActivity));
        PageInfo<SeckillActivityVo> seckillActivityVoPageInfo = CommonBeanUtils.copyPage(pageInfo, new PageInfo<SeckillActivityVo>(), SeckillActivityVo::new);
        seckillActivityVoPageInfo.setList(seckillActivityVos);
        return seckillActivityVoPageInfo;
    }

    @Override
    public int delById(List<Long> seckillActivityId) {
        int num = seckillActivityMapper.deleteById(seckillActivityId);
        if(ObjectUtils.isEmpty(num)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return num;
    }

    @Override
    public int upStatusById(List<Long> seckillActivityId) {
        int num = seckillActivityMapper.updateStatusById(seckillActivityId);
        if(ObjectUtils.isEmpty(num)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return num;
    }

    @Override
    public int upActivityById(List<Long> seckillActivityId) {
        int num = seckillActivityMapper.updateActivityStatusById(seckillActivityId);
        if(ObjectUtils.isEmpty(num)){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return num;
    }
    @Transactional
    @Override
    public int upActivity(SeckillActivityDto seckillActivityDto) {
        //判断活动是否可以修改  只有等待开始状态才能修改
        SeckillActivity seckillActivity = seckillActivityMapper.selectSeckillActivityById(seckillActivityDto.getSeckillActivityId());
        if(ObjectUtils.isEmpty(seckillActivity)){
            //如果为空则说明该活动不是等待状态，或者没有该活动
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        //如果不为空 则修改要修改的活动状态
        int num = seckillActivityMapper.updateActivityById(seckillActivity);
        if(num==0){
            throw new ServiceException(ResponseStatus.SYS_ERROR);
        }
        //传入商品活动的数据进行判断 传入数据的商品id 与 获取对应的商品id进行判断，一样则修改
        List<SeckillActivityProduct> seckillActivityProducts = seckillActivityProductMapper.selectProductById(seckillActivityDto);
        if(seckillActivityProducts.size()>0){
            if(seckillActivityProducts.size()==seckillActivityDto.getActivityProductList().size()){

                //一样就直接修改
                num = seckillActivityProductMapper.updateSeckillActivityProductById(seckillActivityDto);
            }else if(seckillActivityProducts.size()>seckillActivityDto.getActivityProductList().size()){
                //比较少 嵌套for循环让获取的商品id和将要修改的商品id进行对比 如果一样则修改，不一样则添加
                //将要添加
                for (SeckillActivityProduct product : seckillActivityDto.getActivityProductList()) {
                    //原有的商品
                    for (SeckillActivityProduct seckillActivityProduct : seckillActivityProducts) {
                        if(product.getCommodityId().equals(seckillActivityProduct.getCommodityId())){
                            //修改
                            num = seckillActivityProductMapper.updateProduct(product);
                            if(num == 0 ){
                                throw  new ServiceException(ResponseStatus.SYS_ERROR);
                            }
                        }
                        //不一样则添加
                        num = seckillActivityProductMapper.insertProduct(product);
                        if(num == 0 ){
                            throw  new ServiceException(ResponseStatus.DATA_ERROR);
                        }
                    }
                }
            }else {
                //比较多 多了就伪删除
                for (SeckillActivityProduct product : seckillActivityDto.getActivityProductList()) {
                    //原有的商品
                    for (SeckillActivityProduct seckillActivityProduct : seckillActivityProducts) {
                        if(product.getCommodityId().equals(seckillActivityProduct.getCommodityId())){
                            //修改
                            num = seckillActivityProductMapper.updateProduct(product);
                            if(num == 0 ){
                                throw  new ServiceException(ResponseStatus.SYS_ERROR);
                            }
                        }
                        //不一样则添加
                        num = seckillActivityProductMapper.deleteProduct(product);
                    }
                }
            }
        }else {
            //直接添加
            num = seckillActivityProductMapper.insertSeckillActivityProduct(seckillActivityDto);
        }
        if(num == 0 ){
            throw  new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return num;
    }
    @Transactional
    @Override
    public int addActivity(SeckillActivityDto seckillActivityDto) {
        //直接添加活动场次和批量添加商品
        int num = seckillActivityMapper.insertActivity(seckillActivityDto);
        if(num==0){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        num = seckillActivityProductMapper.insertSeckillActivityProduct(seckillActivityDto);
        if(num==0){
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        return num;
    }
}
      /* if(seckillActivity.getActivityProductList().size()!=0){
           //对要进行修改的商品进行 数量判断
           if(seckillActivityDto.getActivityProductList().size()==seckillActivity.getActivityProductList().size()){
               //如果等于 则改变商品信息即可
               num = seckillActivityProductMapper.updateSeckillActivityProductById(seckillActivityDto);
           }else if(seckillActivityDto.getActivityProductList().size()<seckillActivity.getActivityProductList().size()){
               //如果要修改的商品比原商品少，则再进行判断
               while (seckillActivityDto.getActivityProductList().size()<seckillActivity.getActivityProductList().size()){
                   for (SeckillActivityProduct seckillActivityProduct : seckillActivity.getActivityProductList()) {
                       //先减少到一样的数量再进行修改
                       num = seckillActivityProductMapper.deleteSeckillActivityProductById(seckillActivityProduct.getSeckillActivityProductId());
                       if(num==0){
                           throw new ServiceException(ResponseStatus.SYS_ERROR);
                       }
                   }
               }
               //再查询后进行修改
               seckillActivity = seckillActivityMapper.selectSeckillActivityById(seckillActivityDto.getSeckillActivityId());
               if(ObjectUtils.isEmpty(seckillActivity)){
                   throw new ServiceException(ResponseStatus.DATA_ERROR);
               }
               SeckillActivityDto seckillActivityDto1 = CommonBeanUtils.copyForBean(seckillActivity, SeckillActivityDto::new);
               num = seckillActivityProductMapper.updateSeckillActivityProductById(seckillActivityDto1);
           }else if(seckillActivityDto.getActivityProductList().size()>seckillActivity.getActivityProductList().size()){
               //当你修改的商品数量比原来多时，先
           }
       }else {
           //直接添加

       }*/

